<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>危险词库</el-breadcrumb-item>
        <el-breadcrumb-item>危险词汇</el-breadcrumb-item> 
      </el-breadcrumb>
    </basic-container> 
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      >
        <template slot="lexicon" slot-scope="scope">
          
          <span v-for="item in scope.row.lexicon.toString().split(',')" :key="item" v-if="scope.row.type==1" >
            <el-tag type="danger" v-text="item"></el-tag>&nbsp;
          </span> 
          <span v-for="item in scope.row.lexicon.toString().split(',')" :key="item" v-if="scope.row.type==2" >
            <el-tag type="warning" v-text="item"></el-tag>&nbsp;
          </span> 
          <!-- <textarea v-text="lexicon(scope.row.lexicon)"></textarea> -->
        </template>
        <!-- <template slot-scope="scope" slot="menu"> 
          <el-button
            icon="el-icon-edit"
            type="text"
            size="small"
            @click.stop="reply(scope)"
            >编辑</el-button
          >
        </template>  -->
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { appDangerousLexicon as baseURL } from "@/api/cat/baseUrls"; 

export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
        // pageSizes:[1,2,3]
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "createtime&ASC",
      },
      option: {
        delBtn: true,
        editBtn: true,
        searchBtn: true,
        tip:true,
        column: [
          {
            label: "类型",
            prop: "type",
            type: "select",
            search: true,
            placeholder: "类型",
            dicData: [
              {
                label: "高危词",
                value: "1",
              },
              {
                label: "敏感词",
                value: "2",
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
            rules: [
              {
                required: true,
                message: "类型必须选择",
              },
            ],
          },
          {
            label: "词库",
            prop: "lexicon",
            type: "textarea",
            placeholder: "一行一个词或每个词用逗号隔开",
            rules: [
              {
                required: true,
                message: "词库必须填写",
              },
            ],
            tip:"每个词汇,请用英文(,)逗号或换行(一行一个)进行隔开",
            slot: true,
          },

          {
            label: "最后操作信息",
            prop: "edit_time",
            type: "date",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: true,
            readonly: true,
          },
        ],
      },
    };
  },
  methods: {
    // reply (scope) {
    //   let arr = JSON.parse(JSON.parse(scope.row.lexicon));
    //   let str = "";
    //   for (let i = 0; i < arr.length; i++) {
    //     if(i!=arr.length-1){
    //       str += arr[i]+'\n';
    //     }
    //   }
    //   scope.row.lexicon = str;
    //   this.$refs.crud.rowEdit(scope.row, scope.index)
    // },
    lexicon(lexicon){
      let arr = JSON.parse(JSON.parse(lexicon));
      let str = "";
      for (let i = 0; i < arr.length; i++) {
        if(i!=arr.length-1){
          str += arr[i]+'\n';
        }
      }
      return str;
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.pageAndParam,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];
      row.create_time = new Date();
      let userInfo = this.$store.state.user.userInfo;
      //       scope.row.admin_id = userInfo.id ? userInfo.id : null
      //       scope.row.admin_name = userInfo.username
      row.edit_account = userInfo.username;
      row.lexicon = row.lexicon.replace(/\r\n/g, ',').replace(/\r/g, ',').replace(/\n/g, ',').replace(/,{2,}/g, ',');//.​replace(/\r\n/g, ',').replace(/\r/g, ',').replace(/\n/g, ',').replace(/,{2,}/g, ',');
      // let arr = row.lexicon.split(",");
      // row.lexicon = arr;
      let data = await common(baseURL.save, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo;
      row.edit_account = userInfo.username;
      row.edit_time = new Date();
      row.lexicon = row.lexicon.replace(/\r\n/g, ',').replace(/\r/g, ',').replace(/\n/g, ',').replace(/,{2,}/g, ',');//.​replace(/\r\n/g, ',').replace(/\r/g, ',').replace(/\n/g, ',').replace(/,{2,}/g, ',');
      // let arr = row.lexicon.split(",");
      // row.lexicon = arr;
      let data = await common(baseURL.update, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseURL.delete,
            { id: row.id },
            null,
            "GET"
          );
          // if (response == null) {
          //   return ;
          // }
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {
      
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>